from functools import wraps

from flask import request

from src.config.parameter import Parameter
from src.custom.response import Response


# 没有cookie的中间件
def not_cookie_middleware(parameter: "Parameter"):
    def decorator(func):
        @wraps(func)
        def decorated_function(*args, **kwargs):
            finished = False  # 替换为你的逻辑来判断进程是否已完成
            if finished:
                return
            return func(*args, **kwargs)

        return decorated_function

    return decorator


# 有cookie的中间件
def check_cookie_middleware(parameter: "Parameter"):
    def decorator(func):
        @wraps(func)
        def decorated_function(*args, **kwargs):
            cookie = request.headers.get("x-user-cookie")  # 获取请求头部中的 "x-user-cookie" 值
            if not cookie:
                return Response.fail(code=401, message="请先登录")

            # 运行 cookie_object 的方法和更新 cookie
            parameter.cookie_object.run(cookie)
            parameter.update_cookie()
            return func(*args, **kwargs)

        return decorated_function

    return decorator
